package com.mcxx.modules.miniSalvation.check.service.read;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.check.dao.read.CheckReadDao;
import com.mcxx.modules.miniSalvation.check.dto.CheckListExcelDTO;
import com.mcxx.modules.miniSalvation.check.dto.CheckListSearchDTO;
import com.mcxx.modules.miniSalvation.check.dto.QueryCheckDTO;
import com.mcxx.modules.miniSalvation.check.entity.CheckEntity;
import com.mcxx.modules.miniSalvation.check.param.CheckListSearchParam;
import com.mcxx.modules.miniSalvation.order.param.CheckParam;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;




@Service
@TaTransactional(readOnly = true)
public class CheckReadService extends BaseService {

    @Autowired
    private CheckReadDao checkReadDao;
    @Autowired
    private FileService fileService;

    public String getCheckReportFile(String orderId){

        String fileObjectKey = this.getCheckFileObjectKey(orderId);

        String fileKey = fileService.getFileUrl(fileObjectKey);

        if(CommonUtil.invalidArgs(fileKey)){
            throw new BizException(FailureCode.ERR_20058);
        }
        return fileKey;
    }


    public String getCheckFileObjectKey(String orderId){
        return checkReadDao.getCheckFileObjectKey(orderId);
    }


    public List<String> getNoReportFile(List<String> familyIdList){
        return checkReadDao.getNoReportFile(familyIdList);
    }


    public CheckEntity getCheckIdFile(String orderId){
        return checkReadDao.getCheckIdFile(orderId);
    }


    @Deprecated
    public List<CheckEntity> moreConditionQuery(CheckParam param){
        return checkReadDao.moreConditionQuery(param);
    }


    public List<String> verifyFamilyIdCheck(List<String> familyIdList, List<String> idCardList, Map<String, List<String>> idcardCheckRstMsg){

        List<String> noExitCheckFamilyIdList = checkReadDao.queryByFamilyIds(familyIdList);
        if(noExitCheckFamilyIdList.size() > 0){
            idcardCheckRstMsg.put("canNotSendCheck", noExitCheckFamilyIdList);
        }
        return noExitCheckFamilyIdList;
    }


    public int queryByFamilyId(String familyId){
        return checkReadDao.queryByFamilyId(familyId);
    }


    public List<String> selectStandType(List<String> list,String standType){return checkReadDao.selectStandType(list,standType);}


    public int isReturnCheckFile(String orderId) {

        CheckEntity check = checkReadDao.getCheckIdFile(orderId);
        if(CommonUtil.invalidArgs(check) == false && CommonUtil.invalidArgs(check.getCheckId()) == false && CommonUtil.invalidArgs(check.getCheckReportFile())){

           return 0;
        }
        return 1;
    }

    @Deprecated
    public List<String> getCheckFileBatch(List<String> orderId){
        return checkReadDao.getUnfinishCheck(orderId);
    }


    public List<CheckEntity> selectCheckList(String orderId){return checkReadDao.selectCheckList(orderId);}



    public List<String> getNoCompleteFamilyId(List<String> familyIdList){
        return checkReadDao.getNoCompleteFamilyId(familyIdList);
    }


    public int selectExpireRecord(String orderId){
        return checkReadDao.selectExpireRecord(orderId);
    }

    public CheckEntity selectUnFinishCheck(String checkId){
        return checkReadDao.selectUnFinishCheck(checkId);
    }

    public String selectCheckFileType(String checkReportFile){return checkReadDao.selectCheckFileType(checkReportFile);}

    public int decideIncome(String orderId,String familyId){
        return checkReadDao.decideIncome(orderId,familyId);
    }

    public List<CheckEntity> selectCheckListByState(String checkState){return checkReadDao.selectCheckListByState(checkState);}

    public String selectIdcardByCheckId(List<String> idList){
        return checkReadDao.selectIdcardByCheckId(idList);
    }

    public String selectIdcardByBatchNo(String batchNo){
        return checkReadDao.selectIdcardByBatchNo(batchNo);
    }

    public CheckEntity selectById(String checkId){
        return checkReadDao.selectById(checkId);
    }

    public List<CheckListSearchDTO> selectIntegrateCheckList(CheckListSearchParam param){
        return checkReadDao.selectIntegrateCheckList(param);
    }

    public List<CheckListExcelDTO> exportIntegrateCheckList(CheckListSearchParam param){
        return checkReadDao.exportIntegrateCheckList(param);
    }

    public Page selectIntegrateCheckList(CheckListSearchParam param,PageParam pageParam,UserAccountVo accountVo){

        QueryParamUtil.setBaseParamValue(param,accountVo);


        checkReadDao.beginPager(pageParam);
        List<CheckListSearchDTO> list = checkReadDao.selectIntegrateCheckList(param);


        return checkReadDao.endPager(list);
    }

    public Page selectCheckRecordByParam(CheckListSearchParam param,PageParam pageParam,UserAccountVo accountVo){
        checkReadDao.beginPager(pageParam);
        List<CheckEntity> list = checkReadDao.selectCheckRecordByParam(param);

        return checkReadDao.endPager(list);
    }

    public List<CheckEntity> selectUnFinishCheckByOrderId(String orderId){
        return checkReadDao.selectUnFinishCheckByOrderId(orderId);
    }


    public Page<QueryCheckDTO> selectCheckListByCreateTime(PageParam pageParam, int interval) {

        checkReadDao.beginPager(pageParam);
        List<QueryCheckDTO> list = checkReadDao.selectCheckListByCreateTime(interval);


        return checkReadDao.endPager(list);
    }
}
